{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IPython提供了许多魔法命令，使得在IPython环境中的操作更加得心应手。魔法命令都以%或者%%开头，以%开头的成为行命令，%%开头的称为单元命令。行命令只对命令所在的行有效，而单元命令则必须出现在单元的第一行，对整个单元的代码进行处理。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "执行%lsmagic列出所有的魔法命令<br>执行%magic可以查看关于各个命令的说明<br>在命令之后添加?可以查看该命令的详细说明。<br>\n",
    "所有命令的详细解释请见文档 http://ipython.readthedocs.io/en/stable/interactive/magics.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-20T11:16:29.448913Z",
     "start_time": "2018-02-20T11:16:29.435604Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/json": {
       "cell": {
        "!": "OSMagics",
        "HTML": "Other",
        "SVG": "Other",
        "bash": "Other",
        "capture": "ExecutionMagics",
        "debug": "ExecutionMagics",
        "file": "Other",
        "html": "DisplayMagics",
        "javascript": "DisplayMagics",
        "js": "DisplayMagics",
        "latex": "DisplayMagics",
        "perl": "Other",
        "prun": "ExecutionMagics",
        "pypy": "Other",
        "python": "Other",
        "python2": "Other",
        "python3": "Other",
        "ruby": "Other",
        "script": "ScriptMagics",
        "sh": "Other",
        "svg": "DisplayMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "writefile": "OSMagics"
       },
       "line": {
        "alias": "OSMagics",
        "alias_magic": "BasicMagics",
        "autocall": "AutoMagics",
        "automagic": "AutoMagics",
        "autosave": "KernelMagics",
        "bookmark": "OSMagics",
        "cat": "Other",
        "cd": "OSMagics",
        "clear": "KernelMagics",
        "colors": "BasicMagics",
        "config": "ConfigMagics",
        "connect_info": "KernelMagics",
        "cp": "Other",
        "debug": "ExecutionMagics",
        "dhist": "OSMagics",
        "dirs": "OSMagics",
        "doctest_mode": "BasicMagics",
        "ed": "Other",
        "edit": "KernelMagics",
        "env": "OSMagics",
        "gui": "BasicMagics",
        "hist": "Other",
        "history": "HistoryMagics",
        "killbgscripts": "ScriptMagics",
        "ldir": "Other",
        "less": "KernelMagics",
        "lf": "Other",
        "lk": "Other",
        "ll": "Other",
        "load": "CodeMagics",
        "load_ext": "ExtensionMagics",
        "loadpy": "CodeMagics",
        "logoff": "LoggingMagics",
        "logon": "LoggingMagics",
        "logstart": "LoggingMagics",
        "logstate": "LoggingMagics",
        "logstop": "LoggingMagics",
        "ls": "Other",
        "lsmagic": "BasicMagics",
        "lx": "Other",
        "macro": "ExecutionMagics",
        "magic": "BasicMagics",
        "man": "KernelMagics",
        "matplotlib": "PylabMagics",
        "mkdir": "Other",
        "more": "KernelMagics",
        "mv": "Other",
        "notebook": "BasicMagics",
        "page": "BasicMagics",
        "pastebin": "CodeMagics",
        "pdb": "ExecutionMagics",
        "pdef": "NamespaceMagics",
        "pdoc": "NamespaceMagics",
        "pfile": "NamespaceMagics",
        "pinfo": "NamespaceMagics",
        "pinfo2": "NamespaceMagics",
        "popd": "OSMagics",
        "pprint": "BasicMagics",
        "precision": "BasicMagics",
        "profile": "BasicMagics",
        "prun": "ExecutionMagics",
        "psearch": "NamespaceMagics",
        "psource": "NamespaceMagics",
        "pushd": "OSMagics",
        "pwd": "OSMagics",
        "pycat": "OSMagics",
        "pylab": "PylabMagics",
        "qtconsole": "KernelMagics",
        "quickref": "BasicMagics",
        "recall": "HistoryMagics",
        "rehashx": "OSMagics",
        "reload_ext": "ExtensionMagics",
        "rep": "Other",
        "rerun": "HistoryMagics",
        "reset": "NamespaceMagics",
        "reset_selective": "NamespaceMagics",
        "rm": "Other",
        "rmdir": "Other",
        "run": "ExecutionMagics",
        "save": "CodeMagics",
        "sc": "OSMagics",
        "set_env": "OSMagics",
        "store": "StoreMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "tb": "ExecutionMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "unalias": "OSMagics",
        "unload_ext": "ExtensionMagics",
        "who": "NamespaceMagics",
        "who_ls": "NamespaceMagics",
        "whos": "NamespaceMagics",
        "xdel": "NamespaceMagics",
        "xmode": "BasicMagics"
       }
      },
      "text/plain": [
       "Available line magics:\n",
       "%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode\n",
       "\n",
       "Available cell magics:\n",
       "%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile\n",
       "\n",
       "Automagic is ON, % prefix IS NOT needed for line magics."
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%lsmagic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以下为比较常用的命令，排名不分先后"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#魔法命令\" data-toc-modified-id=\"魔法命令-1\">魔法命令</a></span><ul class=\"toc-item\"><li><span><a href=\"#常用的行魔法命令\" data-toc-modified-id=\"常用的行魔法命令-1.1\">常用的行魔法命令</a></span><ul class=\"toc-item\"><li><span><a href=\"#%alias\" data-toc-modified-id=\"%alias-1.1.1\">%alias</a></span></li><li><span><a href=\"#%alias_magic\" data-toc-modified-id=\"%alias_magic-1.1.2\">%alias_magic</a></span></li><li><span><a href=\"#%config\" data-toc-modified-id=\"%config-1.1.3\">%config</a></span></li><li><span><a href=\"#%debug-,-%pdb\" data-toc-modified-id=\"%debug-,-%pdb-1.1.4\">%debug , %pdb</a></span></li><li><span><a href=\"#%env\" data-toc-modified-id=\"%env-1.1.5\">%env</a></span></li><li><span><a href=\"#%history\" data-toc-modified-id=\"%history-1.1.6\">%history</a></span></li><li><span><a href=\"#%load\" data-toc-modified-id=\"%load-1.1.7\">%load</a></span></li><li><span><a href=\"#%load_ext\" data-toc-modified-id=\"%load_ext-1.1.8\">%load_ext</a></span></li><li><span><a href=\"#%logoff,%logon,%logstart,%logstate,%logstop\" data-toc-modified-id=\"%logoff,%logon,%logstart,%logstate,%logstop-1.1.9\">%logoff,%logon,%logstart,%logstate,%logstop</a></span></li><li><span><a href=\"#%matplotlib\" data-toc-modified-id=\"%matplotlib-1.1.10\">%matplotlib</a></span></li><li><span><a href=\"#%pdef\" data-toc-modified-id=\"%pdef-1.1.11\">%pdef</a></span></li><li><span><a href=\"#%pdoc\" data-toc-modified-id=\"%pdoc-1.1.12\">%pdoc</a></span></li><li><span><a href=\"#%pfile\" data-toc-modified-id=\"%pfile-1.1.13\">%pfile</a></span></li><li><span><a href=\"#%pprint\" data-toc-modified-id=\"%pprint-1.1.14\">%pprint</a></span></li><li><span><a href=\"#%precision\" data-toc-modified-id=\"%precision-1.1.15\">%precision</a></span></li><li><span><a href=\"#%prun\" data-toc-modified-id=\"%prun-1.1.16\">%prun</a></span></li><li><span><a href=\"#%psearch\" data-toc-modified-id=\"%psearch-1.1.17\">%psearch</a></span></li><li><span><a href=\"#%psource\" data-toc-modified-id=\"%psource-1.1.18\">%psource</a></span></li><li><span><a href=\"#%pwd\" data-toc-modified-id=\"%pwd-1.1.19\">%pwd</a></span></li><li><span><a href=\"#%pycat\" data-toc-modified-id=\"%pycat-1.1.20\">%pycat</a></span></li><li><span><a href=\"#%pylab\" data-toc-modified-id=\"%pylab-1.1.21\">%pylab</a></span></li><li><span><a href=\"#%store\" data-toc-modified-id=\"%store-1.1.22\">%store</a></span></li><li><span><a href=\"#%quickref\" data-toc-modified-id=\"%quickref-1.1.23\">%quickref</a></span></li><li><span><a href=\"#%rerun\" data-toc-modified-id=\"%rerun-1.1.24\">%rerun</a></span></li><li><span><a href=\"#%reset\" data-toc-modified-id=\"%reset-1.1.25\">%reset</a></span></li><li><span><a href=\"#%reset_selective\" data-toc-modified-id=\"%reset_selective-1.1.26\">%reset_selective</a></span></li><li><span><a href=\"#%run\" data-toc-modified-id=\"%run-1.1.27\">%run</a></span></li><li><span><a href=\"#%time-,-%timeit\" data-toc-modified-id=\"%time-,-%timeit-1.1.28\">%time , %timeit</a></span></li><li><span><a href=\"#%who\" data-toc-modified-id=\"%who-1.1.29\">%who</a></span></li><li><span><a href=\"#%who_ls\" data-toc-modified-id=\"%who_ls-1.1.30\">%who_ls</a></span></li><li><span><a href=\"#%whos\" data-toc-modified-id=\"%whos-1.1.31\">%whos</a></span></li><li><span><a href=\"#%xmode\" data-toc-modified-id=\"%xmode-1.1.32\">%xmode</a></span></li></ul></li><li><span><a href=\"#常用的单元魔法命令\" data-toc-modified-id=\"常用的单元魔法命令-1.2\">常用的单元魔法命令</a></span><ul class=\"toc-item\"><li><span><a href=\"#声明脚本代码类型\" data-toc-modified-id=\"声明脚本代码类型-1.2.1\">声明脚本代码类型</a></span></li><li><span><a href=\"#%%writefile\" data-toc-modified-id=\"%%writefile-1.2.2\">%%writefile</a></span></li><li><span><a href=\"#%%time--%%timeit\" data-toc-modified-id=\"%%time--%%timeit-1.2.3\">%%time  %%timeit</a></span></li><li><span><a href=\"#%%capture\" data-toc-modified-id=\"%%capture-1.2.4\">%%capture</a></span></li></ul></li><li><span><a href=\"#自定义魔法命令\" data-toc-modified-id=\"自定义魔法命令-1.3\">自定义魔法命令</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的行魔法命令 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %alias"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给命令行命令设置别名<br>\n",
    "Define an alias for a system command.<br>\n",
    "`%alias alias_name cmd` defines ‘alias_name’ as an alias for ‘cmd’<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [2]: alias bracket echo \"Input in brackets: <%l>\"\n",
    "In [3]: bracket hello world\n",
    "Input in brackets: <hello world>\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %alias_magic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给魔法命令设置别名<br>\n",
    "Create an alias for an existing line or cell magic."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [1]: %alias_magic t timeit\n",
    "Created `%t` as an alias for `%timeit`.\n",
    "Created `%%t` as an alias for `%%timeit`.\n",
    "\n",
    "In [2]: %t -n1 pass\n",
    "1 loops, best of 3: 954 ns per loop\n",
    "\n",
    "In [3]: %%t -n1\n",
    "   ...: pass\n",
    "   ...:\n",
    "1 loops, best of 3: 954 ns per loop\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %config"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更改jupyter配置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %debug , %pdb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "debug工具<br>\n",
    "notebook 确实提供了debug工具<br>\n",
    "使用方法参见 https://docs.python.org/3/library/pdb.html#debugger-commands <br>\n",
    "只是个人感觉比较鸡肋"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %env"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看、配置环境变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %history"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看历史命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load code into the current frontend.<br>\n",
    "相当于把其他文件(如.py文件)的代码复制在此处"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %load_ext"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load an IPython extension by its module name."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %logoff,%logon,%logstart,%logstate,%logstop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "日志相关命令，可以记录你在notebook中的所有命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "引入matplotlib的变量空间，并选择绘图的后端，常用 %matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-20T11:25:48.498204Z",
     "start_time": "2018-02-20T11:25:48.493791Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available matplotlib backends: ['osx', 'qt4', 'qt5', 'widget', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg', 'agg', 'gtk', 'tk', 'ipympl', 'inline']\n"
     ]
    }
   ],
   "source": [
    "# 查看所有可选择的后端\n",
    "%matplotlib --list   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pdef"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print the call signature for any callable object.<br>\n",
    "If the object is a class, print the constructor information."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pdoc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print the docstring for an object.<br>\n",
    "If the given object is a class, it will print both the class and the constructor docstrings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pfile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出对象被定义的文件的位置<br>\n",
    "Print (or run through pager) the file where an object is defined."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pprint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Toggle pretty printing on/off."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %precision"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set floating point precision for pretty printing.<br>\n",
    "Can set either integer precision or a format string.<br>\n",
    "If numpy has been imported and precision is an int, numpy display precision will also be set, via numpy.set_printoptions.<br>\n",
    "If no argument is given, defaults will be restored."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [1]: from math import pi\n",
    "\n",
    "In [2]: %precision 3\n",
    "Out[2]: u'%.3f'\n",
    "\n",
    "In [3]: pi\n",
    "Out[3]: 3.142\n",
    "\n",
    "In [4]: %precision %i\n",
    "Out[4]: u'%i'\n",
    "\n",
    "In [5]: pi\n",
    "Out[5]: 3\n",
    "\n",
    "In [6]: %precision %e\n",
    "Out[6]: u'%e'\n",
    "\n",
    "In [7]: pi**10\n",
    "Out[7]: 9.364805e+04\n",
    "\n",
    "In [8]: %precision\n",
    "Out[8]: u'%r'\n",
    "\n",
    "In [9]: pi**10\n",
    "Out[9]: 93648.047476082982\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %prun "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检测代码性能，很有用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %psearch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搜索变量、函数等对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%psearch a*            -> objects beginning with an a\n",
    "%psearch -e builtin a* -> objects NOT in the builtin space starting in a\n",
    "%psearch a* function   -> all functions beginning with an a\n",
    "%psearch re.e*         -> objects beginning with an e in module re\n",
    "%psearch r*.e*         -> objects that start with e in modules starting in r\n",
    "%psearch r*.* string   -> all strings in modules beginning with r\n",
    "\n",
    "%psearch -c a*         list all object beginning with lower case a\n",
    "%psearch -a _*         list objects beginning with a single underscore\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %psource"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看关于对象(函数、类)的所有源代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回当前路径<br>\n",
    "Return the current working directory path."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pycat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show a syntax-highlighted file through a pager.<br>\n",
    "This magic is similar to the cat utility, but it will assume the file to be Python source and will show it with syntax highlighting.<br>\n",
    "This magic command can either take a local filename, an url, an history range (see %history) or a macro as argument<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%pycat myscript.py\n",
    "%pycat 7-27\n",
    "%pycat myMacro\n",
    "%pycat http://www.example.com/myscript.py\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %pylab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "便捷地创建科学计算环境<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%pylab [--no-import-all] [gui]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "等价于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "import numpy\n",
    "import matplotlib\n",
    "from matplotlib import pylab, mlab, pyplot\n",
    "np = numpy\n",
    "plt = pyplot\n",
    "\n",
    "from IPython.display import display\n",
    "from IPython.core.pylabtools import figsize, getfigs\n",
    "\n",
    "from pylab import *\n",
    "from numpy import *\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you pass --no-import-all, the last two * imports will be excluded."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %store"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在notebook间共享变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在文件a.ipynb中执行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "data = 'this is the string I want to pass to different notebook'\n",
    "%store data\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在文件.ipynb中执行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%store -r data\n",
    "print(data)\n",
    "\n",
    "# this is the string I want to pass to different notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "即可获得该变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %quickref"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一些快捷使用的参考，强烈推荐初学者看看<br>\n",
    "Show a quick reference sheet.<br>\n",
    "完整版参见 http://ipython.readthedocs.io/en/stable/interactive/reference.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %rerun"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Re-run previous input\n",
    "\n",
    "By default, you can specify ranges of input history to be repeated (as with %history). With no arguments, it will repeat the last line.\n",
    "\n",
    "Options:\n",
    "\n",
    "-l <n> : Repeat the last n lines of input, not including the current command.\n",
    "\n",
    "-g foo : Repeat the most recent line which contains foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %reset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "清空变量空间<br>\n",
    "Parameters\n",
    "\n",
    "-f : force reset without asking for confirmation.\n",
    "\n",
    "-s : ‘Soft’ reset: Only clears your namespace, leaving history intact.\n",
    "References to objects may be kept. By default (without this option), we do a ‘hard’ reset, giving you a new session and removing all references to objects from the current session.\n",
    "in : reset input history\n",
    "\n",
    "out : reset output history\n",
    "\n",
    "dhist : reset directory history\n",
    "\n",
    "array : reset only variables that are NumPy arrays\n",
    "\n",
    "See Also\n",
    "\n",
    "reset_selective : invoked as %reset_selective\n",
    "\n",
    "Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [6]: a = 1\n",
    "\n",
    "In [7]: a\n",
    "Out[7]: 1\n",
    "\n",
    "In [8]: 'a' in _ip.user_ns\n",
    "Out[8]: True\n",
    "\n",
    "In [9]: %reset -f\n",
    "\n",
    "In [1]: 'a' in _ip.user_ns\n",
    "Out[1]: False\n",
    "\n",
    "In [2]: %reset -f in\n",
    "Flushing input history\n",
    "\n",
    "In [3]: %reset -f dhist in\n",
    "Flushing directory history\n",
    "Flushing input history\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %reset_selective"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "清理指定变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行外部文件，非常有用，建议查看文档学习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %time , %timeit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用于测试代码时间，非常有用，建议查看文档学习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %who"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看所有变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [1]: alpha = 123\n",
    "\n",
    "In [2]: beta = 'test'\n",
    "\n",
    "In [3]: %who\n",
    "alpha   beta\n",
    "\n",
    "In [4]: %who int\n",
    "alpha\n",
    "\n",
    "In [5]: %who str\n",
    "beta\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %who_ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同%who,但是返回形式为list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "In [1]: alpha = 123\n",
    "\n",
    "In [2]: beta = 'test'\n",
    "\n",
    "In [3]: %who_ls\n",
    "Out[3]: ['alpha', 'beta']\n",
    "\n",
    "In [4]: %who_ls int\n",
    "Out[4]: ['alpha']\n",
    "\n",
    "In [5]: %who_ls str\n",
    "Out[5]: ['beta']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %whos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回变量对应的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %xmode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改变报错的信息格式<br>\n",
    "Valid modes: Plain, Context and Verbose.<br>\n",
    "If called without arguments, acts as a toggle."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的单元魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 声明脚本代码类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* %%bash         很有用，从此不用写shell文件了\n",
    "* %%html\n",
    "* %%javascript，%%js\n",
    "* %%latex\n",
    "* %%markdown\n",
    "* %%perl\n",
    "* %%pypy\n",
    "* %%python\n",
    "* %%python2\n",
    "* %%python3\n",
    "* %%ruby\n",
    "* %%sh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %%writefile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方便快捷写文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%%writefile [-a] filename\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write the contents of the cell to a file.<br>\n",
    "The file will be overwritten unless the -a (–append) flag is specified."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %%time  %%timeit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%time  %timeit的cell 版本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### %%capture"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "捕获当前cell的输出，并将其保存为对象<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "%%capture result\n",
    "import numpy as np\n",
    "for n in [1000, 5000, 10000, 50000, 100000, 500000]:\n",
    "    arr = np.random.rand(n)\n",
    "    print \"n={0}\".format(n)\n",
    "    %timeit -n20 np.sort(arr)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "print result.stdout\n",
    "\n",
    "# n=1000\n",
    "# 20 loops, best of 3: 127 us per loop\n",
    "# n=5000\n",
    "# 20 loops, best of 3: 746 us per loop\n",
    "# n=10000\n",
    "# 20 loops, best of 3: 1.69 ms per loop\n",
    "# n=50000\n",
    "# 20 loops, best of 3: 9.22 ms per loop\n",
    "# n=100000\n",
    "# 20 loops, best of 3: 19.7 ms per loop\n",
    "# n=500000\n",
    "# 20 loops, best of 3: 110 ms per loop\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自定义魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参见 http://ipython.readthedocs.io/en/stable/config/custommagics.html#defining-magics"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "none",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
